package jsyf.oa.service.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.constant.Const;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.system.WechatContactsDao;
import jsyf.oa.entity.system.WechatContactsEntity;
import jsyf.oa.facade.system.WechatContactsFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jjx on 2018/3/19.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class WechatContactsService implements WechatContactsFacade {

    @Autowired
    private WechatContactsDao wechatContactsDao;

    //添加、修改方法
    @Override
    public String save(String jsonData) {
        WechatContactsEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), WechatContactsEntity.class);
        Integer rows;
        if ("0".equals(entity.getId()) || StringUtils.isBlank(entity.getId())) {
            entity.setDelFlag(0);
            rows = wechatContactsDao.add(entity);
        } else {
            WechatContactsEntity wechatContactsEntity = (WechatContactsEntity) wechatContactsDao.getIBaseEntityById(entity.getId());
            wechatContactsEntity.setUserId(entity.getUserId());
            wechatContactsEntity.setUserName(entity.getUserName());
            wechatContactsEntity.setScreenshotsUrl(entity.getScreenshotsUrl());
            wechatContactsEntity.setContacts(entity.getContacts());
            rows = wechatContactsDao.update(wechatContactsEntity);
        }
        return rows == 0 ? "" : entity.getId();
    }

    //逻辑删除方法
    @Override
    public boolean delById(String jsonData) {
        WechatContactsEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), WechatContactsEntity.class);
        int flag = wechatContactsDao.delById(entity.getId());
        if (Const.INT_ONE == flag) {
            return true;
        } else {
            return false;
        }
    }

    //查询方法
    @Override
    public JSONObject getByUserId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //查询list
        List<JSONObject> wechatContactsList = wechatContactsDao.getByUserId(jsonParams.getString("userId"), requestPage, pageSize);
        String condition = "AND DEL_FLAG = 0 ";
        if (jsonParams.containsKey("userId")) {
            condition += "AND USER_ID='" + jsonParams.getString("userId") + "'";
        }
        //查询总数
        Integer recordCount = wechatContactsDao.getPageCount(condition, new Object[]{});
        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            if (recordCount % pageSize != 0) {
                double flag = Math.ceil(recordCount / pageSize + 1);
                pageCount = ((int) flag);
            } else {
                double flag = Math.ceil(recordCount / pageSize);
                pageCount = ((int) flag);
            }
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", wechatContactsList);
        return result;
    }

    @Override
    public JSONObject getByUserName(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //查询list
        List<JSONObject> wechatContactsList = wechatContactsDao.getByUserName(jsonParams.getString("userName"), requestPage, pageSize);
        String condition = "AND DEL_FLAG = 0 ";
        if (jsonParams.containsKey("userName")) {
            condition += "AND USER_Name='" + jsonParams.getString("userName") + "'";
        }
        //查询总数
        Integer recordCount = wechatContactsDao.getPageCount(condition, new Object[]{});
        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            if (recordCount % pageSize != 0) {
                double flag = Math.ceil(recordCount / pageSize + 1);
                pageCount = ((int) flag);
            } else {
                double flag = Math.ceil(recordCount / pageSize);
                pageCount = ((int) flag);
            }
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", wechatContactsList);
        return result;
    }

    @Override
    public JSONObject getContactsByCompanyId(JSONObject jsonObject) {
        String companyId = jsonObject.get("companyId").toString();
        JSONObject result = wechatContactsDao.getContactsByCompanyId(companyId);

        return result;
    }
    @Override
    public List<JSONObject> getContactsCompanyList(String jsonData){
        List<JSONObject> result = new ArrayList<>();
        result = wechatContactsDao.getCompanyList();
        return result;
    }

    @Override
    public List<JSONObject> getWechatContactsTotal(JSONObject jsonData) {
        List<JSONObject> list = wechatContactsDao.getCompanyList();
        List<JSONObject> result = new ArrayList<>();
        for (int i=0; i<list.size(); i++){
            String companyId = list.get(i).getString("id");
            String companyName = list.get(i).getString("name");
            JSONObject jsonObject = wechatContactsDao.getContactsByCompanyId(companyId);
            jsonObject.put("companId",companyId);
            jsonObject.put("companyName",companyName);
            result.add(jsonObject);

        }
         return  result;
    }
}
